using Gameplay.PVE.Entity;
using UnityEngine;
using Yoozoo.Managers;

namespace Gameplay.PVE
{
    public class HangingMoveComponent:MoveComponent
    {
        protected override void OnUpdate()
        {
            if (!PveManager.Instance.isInPve && !PveManager.Instance.isToRpg)
            {
                return;
            }
            unit.Data.position = unit.gameObject.transform.position;
            return;
            base.OnUpdate();
            unit.Data.moveState = 1;
            unit.Data.manualForward = unit.Data.targetForward;
            UpdatePosition();
            UpdateForward();
            unit.gameObject.transform.position = unit.Data.position;
            unit.gameObject.transform.forward = unit.Data.forward;
        }

        protected override void OnTransmit(ETransmitType type, BaseTransmitArg arg)
        {
            if (!PveManager.Instance.isInPve && !PveManager.Instance.isToRpg)
            {
                return;
            }
            if (type == ETransmitType.StageStart)
            {
                if (unit.Data.team == 0)
                {
                    if (unit.Data.id == 0)//主角，巡逻逻辑
                    {
                        //if (!unit.Data.isManualMoving)
                        {
                            unit.Data.ClearOrders();
                            /*var list = PveManager.Instance.GetMyRoadPath(unit.Data.id);
                            for (int i = 1; i < list.Count; i++)
                            {
                                var patrol = ClassManager.Get<UnitOrder>();
                                patrol.targetPosition = list[i];
                                patrol.type = UnitOrder.EOrderType.Patrol;
                                unit.Data.AddOrder( patrol);
                                if (i == 1)
                                {
                                    ObeyOrder(patrol);
                                }
                            }*/
                        }

                    }
                    else//小弟 跟随
                    {
                        var follow = ClassManager.Get<UnitOrder>();
                        follow.target = UnitManager.Instance.GetAfkMainUnit();
                        follow.type = UnitOrder.EOrderType.Follow;
                        unit.Data.AddOrder(follow);
                        ObeyOrder(follow);
                    }

                }
            }
            else if (type == ETransmitType.StageToNext)
            {
                if (unit.Data.id != 0)
                {
                    return;
                }
                unit.Data.ClearOrders();
                //unit.Data.SetIsInAttack(false);
                unit.Data.SetLockTarget(null);
                var birthPosition = Vector3.zero;
                unit.Data.SetTargetPosition(birthPosition);
                unit.Data.patrolPosition = birthPosition;
                unit.Data.birthPosition = birthPosition;
                unit.Data.isInBirthPosition = false;
                if (unit.Data.isManualMoving && PveManager.Instance.isToRpg)
                {
                    unit.Data.angleSpeed = 1000;
                }
            }
            else if (type == ETransmitType.GoToRpg)
            {
                if (unit.Data.id != 0)
                {
                    return;
                }
                unit.Data.ClearOrders();
                var list = PveManager.Instance.GetMyRoadPathToRpg(unit.Data.id);
                for (int i = 0; i < list.Count; i++)
                {
                    var patrol = ClassManager.Get<UnitOrder>();
                    patrol.targetPosition = list[i];
                    patrol.type = UnitOrder.EOrderType.Patrol;
                    unit.Data.AddOrder( patrol);
                    if (i == 0)
                    {
                        ObeyOrder(patrol);
                    }
                }
                unit.Data.birthPosition = list[list.Count - 1];
            }
            base.OnTransmit(type,arg);
        }

        private Vector3 moveAttackManualForward;
        private float moveAttackManualForwardTime;
        
        //检查是否到巡逻点，或是是否到攻击点
        protected void CheckPatrol()
        {
            var orders = unit.Data.GetOrders();
            if (orders.Count > 0)
            {
                var currentOrder = orders[0];
                if (currentOrder.type == UnitOrder.EOrderType.Patrol && Vector3.Distance(unit.Data.position, currentOrder.targetPosition) < 1.5f)
                {
                    orders.RemoveAt(0);
                    if (orders.Count > 0)
                    {
                        ObeyOrder(orders[0]);
                    }
                }
                else if (currentOrder.type == UnitOrder.EOrderType.AttackToTarget && unit.Data.IsAttackInRange())
                {
                    orders.RemoveAt(0);
                }
            }
        }
    }
}
